package cn.tedu.sp06;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @EnableDiscoveryClient 启用 springcloud
 * @EnableCircuitBreaker 启用 hystrix断路器
 */
//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker

/**
 * @SpringCloudApplication 可以代替上面三个注解
 */
@SpringCloudApplication
public class Sp06RibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp06RibbonApplication.class, args);
	}

	/**
	 * 创建RestTemplate实例，并存入spring容器
	 * 可以放在启动类中，或者可以放在自定义的自动配置类中
	 *
	 * @LoadBalanced ribbon的注解，对RestTemplate进行增强
	 */
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate(){
		SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory();
		//连接的超时
		f.setConnectTimeout(1000);
		//等待响应的超时
		f.setReadTimeout(1000);
		return new RestTemplate(f);
	}

}
